package com.heu.blood.transfusion.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.heu.blood.common.aop.ControllerLogAnnotation;
import com.heu.blood.common.utils.GenerateNumberBySnowFlake;
import com.heu.blood.common.utils.R;
import com.heu.blood.common.utils.PageUtils;
import com.heu.blood.send.vo.BloodInformationAndSendVo;
import com.heu.blood.transfusion.entity.BloodAdverseReactionEntity;
import com.heu.blood.transfusion.entity.BloodAdverseReactionInfo;
import com.heu.blood.transfusion.entity.BloodAdverseReactionVo;
import com.heu.blood.transfusion.service.BloodAdverseReactionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import static com.heu.blood.common.Constant.ConstantData.ADVERSE_NO_PREFIX;
import static com.heu.blood.common.Constant.ConstantData.ADVERSE_NO_WORK_ID;


/**
 * 
 *
 * @author lhh
 * @email lhh1612325771@163.com
 * @date 2023-08-28 15:21:02
 */
@RestController
@Slf4j
@RequestMapping("transfusion/bloodAdverseReaction")
public class BloodAdverseReactionController {
    @Autowired
    private BloodAdverseReactionService bloodAdverseReactionService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @ControllerLogAnnotation(module = "血液不良反应", operate = "分页查询血液不良反应信息")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = bloodAdverseReactionService.queryPage(params);

        return R.ok().put("page", page);
    }



    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @ControllerLogAnnotation(module = "血液不良反应", operate = "根据id查询血液不良反应信息")
    public R info(@PathVariable("id") Long id){
        System.out.println(id);
		BloodAdverseReactionEntity bloodAdverseReaction = bloodAdverseReactionService.getById(id);
        System.out.println(bloodAdverseReaction);
        return R.ok().put("data", bloodAdverseReaction);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @ControllerLogAnnotation(module = "血液不良反应", operate = "保存血液不良反应信息")
    public R save(@RequestBody BloodAdverseReactionEntity bloodAdverseReaction){
        if (!Strings.isNotBlank(bloodAdverseReaction.getAdverseReactionsNo())) {
            GenerateNumberBySnowFlake adverseReactionsNo =new GenerateNumberBySnowFlake(ADVERSE_NO_WORK_ID ,ADVERSE_NO_PREFIX);
            log.info(adverseReactionsNo.genNumberByflag());
            bloodAdverseReaction.setAdverseReactionsNo(adverseReactionsNo.genNumberByflag());
        }
        bloodAdverseReactionService.saveOrUpdate(bloodAdverseReaction);
        return R.ok("提交输血不良反应成功");
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @ControllerLogAnnotation(module = "血液不良反应", operate = "修改血液不良反应信息")
    public R update(@RequestBody BloodAdverseReactionEntity bloodAdverseReaction){
		bloodAdverseReactionService.updateById(bloodAdverseReaction);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @ControllerLogAnnotation(module = "血液不良反应", operate = "批量删除血液不良反应信息")
    public R delete(@RequestBody Long[] ids){
		bloodAdverseReactionService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }
//getAdverseReactionInfo
    @RequestMapping("/getAdverseReactionInfo")
    @ControllerLogAnnotation(module = "血液不良反应", operate = "获取有关血液不良反应信息有关的信息")
    public R getAdverseReactionInfo(String bloodId){
        List<BloodAdverseReactionInfo> adverseReactionInfo = bloodAdverseReactionService.getAdverseReactionInfo(bloodId);

        return R.ok().put("data", adverseReactionInfo);
    }

    /**
     * 根据bloodId查询输血后反馈结果
     * @param bloodId
     * @return
     */
    @RequestMapping("/getReactionWithBloodId")
    @ControllerLogAnnotation(module = "血液不良反应", operate = "根据bloodId查询输血后反馈结果")
    public R getReactionWithBloodId(String bloodId){
        BloodAdverseReactionEntity res = bloodAdverseReactionService.getReactionWithBloodId(bloodId);
        return R.ok().put("data",res);
    }

    /**
     * 根据申请项Id查询全部输血反馈
     * @param applicationItemId
     * @return
     */
    @RequestMapping("/getReactionWithApplicationItemId")
    @ControllerLogAnnotation(module = "血液不良反应", operate = "根据申请项Id查询全部输血反馈")
    public R getReactionWithApplicationItemId(Integer applicationItemId){
        List<BloodAdverseReactionEntity> res = bloodAdverseReactionService.getReactionWithApplicationItemId(applicationItemId);
        return R.ok().put("data",res);
    }
}
